/*
 * Copyright (C) 2009 Jiannan Lu
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.restsql.atom.util;

import java.sql.SQLException;
import restsql.Main;
import com.restsql.db.Database;
import java.util.ArrayList;
/**
 *
 * @author L
 */
public class CollectionUtil {

    public CollectionUtil() {
    }

//    <service xmlns="..." xmlns:atom="http://www.w3.org/2005/Atom">
//  <workspace>
//    <atom:title>My Weblog</atom:title>
//    <collection href="http://www.example.org/blog/entries">
//      <atom:title>Entries</atom:title>
//      <accept>entry</accept>
//    </collection>
//    <collection href="http://www.example.org/blog/photos">
//      <atom:title>Photos</atom:title>
//      <accept>image/*</accept>
//    </collection>
//  </workspace>
//</service>

    public static String TablesToCollection(Database db) throws SQLException
    {
        
        String[] tableStrs = db.getTableInfo().getNameArray();
        
        String rootURL = Main.HOST_URL;
        StringBuilder collectionStr = new StringBuilder();
        if(tableStrs.length>0){
            collectionStr.append("<?xml version='1.0' encoding='UTF-8'?>");
            collectionStr.append("<service>");// xmlns='http://www.w3.org/2007/app' xmlns:atom='http://www.w3.org/2005/Atom'>");
            collectionStr.append("<workspace>");
            collectionStr.append("<title>"+db.getSchemaName()+"</title>");//<atom:title>Tables</atom:title>
            for(String tablename:tableStrs){
                collectionStr.append("<collection href='"+rootURL+"/"+tablename+"'>");
                collectionStr.append("<title>"+tablename+"</title>");
                collectionStr.append("<accept>entry</accept>");

                ArrayList arr = new ArrayList();
                for(String link_tablename:tableStrs){                   
                    if(db.getForeignKeyInfo().areRelated(tablename, link_tablename)){
                        if(!arr.contains(db)){
                            arr.add(tablename+"_"+link_tablename);
                            arr.add(link_tablename+"_"+tablename);
                            collectionStr.append("<link ");
                            //collectionStr.append("rel='" +tablename+"{-}"+link_tablename+"' ");
                            collectionStr.append("rel='"+link_tablename+"' ");
                            collectionStr.append("type='" + "application/collection+xml"+ "' ");
                            collectionStr.append("href='"+rootURL+"/"+link_tablename+"'");
                            collectionStr.append(" />");
       
                        }
                    }
                }
                collectionStr.append("</collection>");
            }
            collectionStr.append("</workspace>");
            collectionStr.append("</service>");
        }
            
        return collectionStr.toString();
    }

}
